package io.gitee.xyuesheng.day05;

public class MyLinkedList implements LinkedList {
    private Element head = null;

    @Override
    public void add(String s) {
        if (head != null) {
            Element tail = head;

            while (tail.next != null) {
                tail = tail.next;
            }
            Element e = new Element(s, null);
            tail.next = e;

        } else {
            head = new Element(s, null);
        }
    }

    @Override
    public String get(int index) {
        if (index < 0) {
            throw new RuntimeException("下标不能小于零");
        }

        Element node = head;

        int count = 0;
        while (node != null) {
            if (count == index) {
                return node.data;
            }
            node = node.next;
            count++;
        }

        return null;
    }

    @Override
    public void add(int index, String s) {
        if (index < 0) {
            throw new RuntimeException("下标不能小于0");
        }

        Element target = new Element(s, null);

        if (index == 0) {
            if (head == null) {
                head = target;
            } else {
                target.next = head;
            }
        } else {
            // 找到index前面的一个元素
            Element node = head;

            for (int i = 1; i < index; i++) {
                if (node != null) {
                    node = node.next;
                } else {
                    break;
                }
            }

            if (node != null) {
                target.next = node.next;
                node.next = target;
            }
        }
    }

    @Override
    public void remove(String s) {
        Element prev = null;
        Element next = head;
        while (next != null) {
            if (s.equals(next.data)) {
                if (prev != null) {
                    prev.next = next.next;
                } else {
                    head = next.next;
                }
                break;
            }
            prev = next;
            next = next.next;
        }
    }

    @Override
    public void remove(int index) {
        if (index < 0) {
            throw new RuntimeException("参数位置无效");
        }

        // 如果删除第一个
        if (index == 0) {
            if (head != null) {
                head = head.next;
            }
        } else {
            // 找到index前面的元素
            Element e = head;
            for (int i = 1; i < index; i++) {
                if (e != null) {
                    e = e.next;
                } else {
                    break;
                }
            }
            if (e != null) {
                if (e.next != null) {
                    e.next = e.next.next;
                }
            }
        }

    }

    @Override
    public int size() {
        Element node = head;
        int count = 0;
        while (node != null) {
            node = node.next;
            count++;
        }
        return count;
    }

    static class Element {
        private String data;
        private Element next;

        public Element(String data, Element next) {
            this.data = data;
            this.next = next;
        }
    }

    @Override
    public void remove1(int index) {
        if (index < 0) {
            throw new RuntimeException("参数位置无效");
        }
        Element fast = head;
        Element slow = head;
        while (index > 0) {
            fast = fast.next;
            index--;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow = slow.next.next;
    }

}
